package com.upbos.upm.settings;

import com.upbos.data.core.SingleDao;
import com.upbos.data.persistence.Where;
import com.upbos.upm.entity.User;
import com.upbos.upm.entity.UserCfg;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class SettingsDao {

    @Resource()
    private SingleDao singleDao;

    public User getUser(String uid) {
        User user = this.getUserWithPassword(uid);
        user.setPassword(null);
        return user;
    }

    public User getUserWithPassword(String uid) {
        return singleDao.queryOne("SettingsDaoMapper.getUser", uid);
    }

    public void updateUser(User user) {
        String[] ignoreProps = {"loginName","password", "email", "status","type", "extCode"};
        singleDao.updateById(user, ignoreProps);
    }

    public void insertUserCfg(String uid, List<UserCfg> cfgs) {
        for (UserCfg cfg : cfgs) {
            cfg.setUid(uid);
        }
        singleDao.batchInsert(cfgs);
    }

    public void deleteUserCfg(String uid, List<UserCfg> cfgs) {
        if (cfgs == null || cfgs.size() == 0) {
            return;
        }
        for (UserCfg cfg : cfgs) {
            cfg.setUid(uid);
            singleDao.delete(cfg, new Where("user_id = #{uid} and key_ = #{key}"));
        }
    }


}
